package com.yxym.generic;

import java.util.*;

@SuppressWarnings({"all"})
public class GenericExercise {
    public static void main(String[] args) {

        //1.HashSet
        HashSet<Student> students = new HashSet<Student>();
        students.add(new Student("小白",1000));
        students.add(new Student("小青",1000));
        students.add(new Student("小黑",2000));
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            Student student =  iterator.next();
            System.out.println(student);
        }
        System.out.println("===========");

        for (Student student :students) {
            System.out.println(student);
        }


        //2.HashMap
        HashMap< String ,Student> studentHashMap = new HashMap<String,Student>();
        /*
            public Set<Map.Entry<K,V>> entrySet() {
                Set<Map.Entry<K,V>> es;
                return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
            }
         */
        //使用key value 进行遍历
        //K-> String V->Student
        studentHashMap.put("小白",new Student("小白",200));
        studentHashMap.put("小青",new Student("小青",200));
        studentHashMap.put("小黑",new Student("小黑",200));
        //使用迭代器方法
        Set <Map.Entry<String,Student>> entrySet = studentHashMap.entrySet();
        Iterator<Map.Entry<String, Student>> iterator1 = entrySet.iterator();
        /*
          public final Iterator<Map.Entry<K,V>> iterator() {
            return new EntryIterator();
            }
         */
        System.out.println("===========");
        while (iterator1.hasNext()) {
            Map.Entry<String, Student> next =  iterator1.next();
            System.out.println(next.getKey());
        }
    }
}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}